<template>
  <div class="app-container" v-loading="isSyncLoading" :element-loading-text="isSyncLoadingText">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="销售出库单号" prop="salesOutboundOrderNo">
        <el-input v-model="queryParams.salesOutboundOrderNo" placeholder="请输入销售出库单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单据编号" prop="fbillno">
        <el-input v-model="queryParams.fbillno" placeholder="请输入单据编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="日期" prop="fdate">
        <el-date-picker clearable v-model="queryParams.fdate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择收料日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="客户" prop="clientName">
        <el-input v-model="queryParams.clientName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单据类型" prop="fType">
        <el-select v-model="queryParams.fType" placeholder="请选择单据类型" clearable>
          <el-option v-for="dict in orderType" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="请检状态" prop="fdocumentStatus">
        <el-select v-model="queryParams.fdocumentStatus" placeholder="请选择单据状态" clearable>
          <el-option v-for="dict in dict.type.mes_workorder_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" @click="synchronization"
          v-hasPermi="['wms:delivery:syncData']" size="mini">同步数据</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="deliverynoticeList" border resizable height="calc(100vh - 230px)">
      <el-table-column type="index" label="序号" align="center" width="50" />
      <el-table-column label="销售出库单号" align="center" prop="salesOutboundOrderNo" width="120" />
      <el-table-column label="单据编号" align="center" prop="fbillno" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleView(scope.row)">{{
            scope.row.fbillno
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="日期" align="center" prop="fdate" width="120" />
      <el-table-column label="客户" min-width="180" align="center" prop="clientName" />
      <el-table-column label="合同号" min-width="155" align="center" prop="f_Tbib_Assistant" />
      <el-table-column label="单据类型" align="center" prop="ftypeName" />
      <el-table-column label="单据状态" align="center" prop="fdocumentStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_workorder_status" :value="scope.row.fdocumentStatus" />
        </template>
      </el-table-column>
      <el-table-column label="创建日期" width="155px" align="center" prop="fcreateDate" />
      <el-table-column label="审核日期" width="155px" align="center" prop="fapproveDate" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleCreateOQC(scope.row)" v-if="
            scope.row.isFinishQuality == '1' &&
            scope.row.isGenerateWarehousing == '0'
          " :disabled="scope.row.isGenerateWarehousing == '1' ? true : false"
            v-hasPermi="['wms:delivery:outStock']">销售出库</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="scope.row.isCheckCompleted != '1'"
            @click="handleRequisition(scope.row)" v-hasPermi="['wms:delivery:check']">发货请检单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <Details ref="Details" :id="parentId" :row="row"></Details>
    <!-- 生成领料单页面 -->
    <ProcessMaintenance v-if="openRequisitionDetail" ref="processMaintenance" @getList="getList"></ProcessMaintenance>
    <SaleOut v-if="openOut" ref="saleOuts" @getList="getList"></SaleOut>
  </div>
</template>

<script>
import {
  listDeliverynotice,
  getDeliverynotice,
  delDeliverynotice,
  addDeliverynotice,
  updateDeliverynotice,
  warehousingDeliverynotice,
  synchronization,
} from "@/api/mes/wm/deliveryGoods";
import Details from "./details.vue";
import SaleOut from "./saleOut.vue";
// 生成领料单页面
import ProcessMaintenance from "./processMaintenance.vue";
export default {
  name: "Deliverygoods",
  dicts: ["mes_workorder_status", "sys_fentry_status"],
  components: { Details, ProcessMaintenance, SaleOut },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      parentId: "",
      // 总条数
      total: 0,
      // 发货通知单头表格数据
      deliverynoticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      orderType: [
        {
          label: '发货通知单',
          value: 1,
        },
        {
          label: '普通预测单',
          value: 3,
        },
      ],
      // 查询参数
      queryParams: {
        fType: null,
        pageNum: 1,
        pageSize: 10,
        fid: null,
        fbillno: null,
        fdocumentStatus: null,
        fdate: null,
        fcustomerId: null,
        fdeliveryDeptId: null,
        fstockerGroupId: null,
        fstockerId: null,
        fsaleDeptId: null,
        fsaleGroupId: null,
        fsalesManId: null,
        fcarrierId: null,
        fcarriageNo: null,
        freceiverId: null,
        fsettleId: null,
        fpayerId: null,
        fcreatorId: null,
        fcreateDate: null,
        fapproveDate: null,
        fcancelDate: null,
        fbillTypeId: null,
        fapproverId: null,
        fcancellerId: null,
        fcancelStatus: null,
        fownerTypeIdHead: null,
        fownerIdHead: null,
        fcloseStatus: null,
        fcloserId: null,
        fcloseDate: null,
        freceiptconditionid: null,
        fheadDeliveryWay: null,
        fisSysClose: null,
        fbussinessType: null,
        freceiveAddress: null,
        fheadLocId: null,
        fcreditCheckResult: null,
        freceiverContactId: null,
        flinkMan: null,
        flinkPhone: null,
        fcloseReason: null,
        fTbibAssistant: null,
        fTbibCombo: null,
        fTlsnText: null,
        isGenerateQuality: null,
        isFinishQuality: null,
        isGenerateWarehousing: null,
        fnote: null,
        clientName: null,
        salesOutboundOrderNo: null,
      },
      row: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fid: [
          {
            required: true,
            message: "金蝶云发货通知单fid不能为空",
            trigger: "blur",
          },
        ],
      },
      //生产领料单是否展示
      openRequisitionDetail: false,
      openOut: false,
      isSyncLoading: false,
      isSyncLoadingText: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询发货通知单头列表 */
    getList() {
      this.loading = true;
      listDeliverynotice(this.queryParams).then((response) => {
        this.deliverynoticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fid: null,
        fbillno: null,
        fdocumentStatus: null,
        fdate: null,
        fcustomerId: null,
        fdeliveryDeptId: null,
        fstockerGroupId: null,
        fstockerId: null,
        fsaleDeptId: null,
        fsaleGroupId: null,
        fsalesManId: null,
        fcarrierId: null,
        fcarriageNo: null,
        freceiverId: null,
        fsettleId: null,
        fpayerId: null,
        fcreatorId: null,
        fcreateDate: null,
        fapproveDate: null,
        fcancelDate: null,
        fbillTypeId: null,
        fapproverId: null,
        fcancellerId: null,
        fcancelStatus: null,
        fownerTypeIdHead: null,
        fownerIdHead: null,
        fcloseStatus: null,
        fcloserId: null,
        fcloseDate: null,
        freceiptconditionid: null,
        fheadDeliveryWay: null,
        fisSysClose: null,
        fbussinessType: null,
        freceiveAddress: null,
        fheadLocId: null,
        fcreditCheckResult: null,
        freceiverContactId: null,
        flinkMan: null,
        flinkPhone: null,
        fcloseReason: null,
        fTbibAssistant: null,
        fTbibCombo: null,
        fTlsnText: null,
        isGenerateQuality: null,
        isFinishQuality: null,
        isGenerateWarehousing: null,
        fnote: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        salesOutboundOrderNo: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleView(row) {
      // this.row = row;
      // this.parentId = row.id;
      // console.log(this.parentId);
      // this.$refs.Details.showFlag = true;
      this.$nextTick(() => {
        this.$refs.Details.init(row);
      });
    },
    /** 生成IQC检验单按钮操作*/
    handleCreateOQC(row) {
      this.openOut = true;
      console.log(row, "销售出库");
      this.$nextTick(() => {
        this.$refs.saleOuts.init(row);
      });
      // const id = row.id;
      // this.$modal.confirm("确认执行销售出库？").then(function() {
      //   return warehousingDeliverynotice(id)
      //   // return execute(issueIds)//执行入库
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("出库成功");
      // }).catch(() => {});
    },
    /** 同步按钮操作 */
    synchronization() {
      this.isSyncLoading = true
      this.isSyncLoadingText = '正在同步中，请稍后……'
      synchronization().then((response) => {
        this.isSyncLoading = false
        this.$modal.msgSuccess("同步发货通知单成功");
        this.getList();
      }).catch(() => {
        this.isSyncLoading = false
      });
    },
    /** 领料单生产按钮操作 */
    handleRequisition(row) {
      this.reset();
      this.openRequisitionDetail = true;
      console.log(row, "领料单编号的row");
      this.$nextTick(() => {
        this.$refs.processMaintenance.init(row);
      });
    },
  },
};
</script>
